<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class HuasysController extends AdminbaseController {
    
    public function _initialize() {
        
    }
    
    public function index() {
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by id desc';
        $idDesc='desc';
        if (isset($_GET['orderby'])){
            $order=$_GET['orderby'];
            if ('desc' == $_GET['orrule']){
                $idDesc='asc';
            }
            $queryDesc='order by id '.$idDesc;
        }
        $postData=array();
        $postData['idDesc']=$idDesc;
       
        $Model = M();
        $query=$Model->query("SELECT count(id) as count FROM `hua_top`  ");
        $count=$query[0]['count'];
        $page = $this->page($count, 10, $p);
        $queryData=$Model->query("SELECT * FROM `hua_top`  ".$queryDesc." limit ".(($p-1)*10).",10 ");
        
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("queryData", $queryData);
        
        $this->display();
    }
    
  
    public function delete() {

        if (isset($_GET['id'])){
            $id=$_GET['id'];
            $Model = M();
            $slider = M("top", 'hua_');
            $slideInfo=$Model->query("SELECT * FROM `hua_top` where id='".$id."' limit 0,1 ");
            $pic='./data/upload/'.$slideInfo[0]['avatar'];
            @unlink($pic);
             
            $slider->where('id='.$id)->delete();
            $info=json_encode(array('state'=>'success','info'=>'删除成功','referer'=>'admin.php?g=Admin&m=Huasys&a=index'));
            echo $info;
        }
    }
    
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $Model = M();
        $slideInfo=$Model->query("SELECT * FROM `hua_top` where id='".$id."' limit 0,1 ");
        $slideInfo[0]['piclink']='data/upload/'.$slideInfo[0]['avatar'];
        $slideInfo[0]['pic']=$slideInfo[0]['avatar'];
        $this->assign("slideInfo", $slideInfo[0]);
        $this->display();
   }
    
    public function add() {
        
        $this->display();
    }
    
    public function editpost() {
        if (isset($_POST['pic'])){
        	$slider = M("top", 'hua_');
        	$data['avatar'] = addslashes(trim($_POST['pic']));
        	$data['msg'] = addslashes(trim($_POST['msg']));
        	$data['link'] = addslashes(trim($_POST['link']));
        	$data['isshow'] =  addslashes(trim($_POST['isshow']));
        	if('yes' == $data['isshow']){
        		$Model = M();
        		$Model->execute("update `hua_top` set isshow='no' ");
        	}
            $id=$_POST['option_id'];
            $slider->where('id='.$id)->save($data);
            
            $info=json_encode(array('status'=>'yes'));
            echo $info;
            
        }
        
    }
    
  
    public function addpost() {
        if (isset($_POST['pic']) && !empty($_POST['pic'])){
            $slider = M("top", 'hua_');
            $data['avatar'] = addslashes(trim($_POST['pic']));
            $data['msg'] = addslashes(trim($_POST['msg']));
            $data['link'] = addslashes(trim($_POST['link']));
            $data['isshow'] =  addslashes(trim($_POST['isshow']));
            if('yes' == $data['isshow']){
            	$Model = M();
            	$Model->execute("update `hua_top` set isshow='no' ");
            }
            $data['createtime']=date("Y-m-d H:i:s");
            $slider->add($data);
            $info=json_encode(array('status'=>'success'));
            echo $info;
        }else{
        	$info=json_encode(array('status'=>'nopic'));
        	echo $info;
        }
        
    }
    
}
